import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'history', base: process.env.BASE_URL, routes: [
        {
            path: "/",
            component: () => import("../viewsAdmin/index.vue"),
            redirect: "index",
            children: [
                {
                    path: "index",
                    component: () => import("../viewsAdmin/base/layout.vue"),
                    redirect: "/index/dashboard",
                    children: [
                        {
                            path: "dashboard",
                            component: () => import("../viewsAdmin/index/dashboard.vue"),
                        },

                    ]
                },
                {
                    path: "adoption/farm",
                    component: () => import("../viewsAdmin/index/adoption/farm/index.vue"),
                    meta: {breadCrumb: ["认养中心", "农场管理"]}
                },
                {
                    path: "adoption/region",
                    component: () => import("../viewsAdmin/index/adoption/region/index.vue"),
                    meta: {breadCrumb: ["认养中心", "区域管理"]}
                },
                {
                    path: "content/manage",
                    component: () => import("../viewsAdmin/index/content/manage/index.vue"),
                    meta: {breadCrumb: ["内容中心", "公告管理"]}
                },
                {
                    path: "content/type",
                    component: () => import("../viewsAdmin/index/content/type/index.vue"),
                    meta: {breadCrumb: ["内容中心", "攻略类型"]}
                },
                {
                    path: "content/play",
                    component: () => import("../viewsAdmin/index/content/play/index.vue"),
                    meta: {breadCrumb: ["内容中心", "游玩攻略"]}
                },
                {
                    path: "setting/staffManage",
                    component: () => import("../viewsAdmin/index/setting/staffManage/index.vue"),
                    meta: {breadCrumb: ["配置中心", "员工管理"]}
                },
                {
                    path: "setting/staffRole",
                    component: () => import("../viewsAdmin/index/setting/staffRole/index.vue"),
                    meta: {breadCrumb: ["配置中心", "员工角色"]}
                },
                {
                    path: "bg/page",
                    component: () => import("../viewsAdmin/index/setting/bg/bgPage/index.vue"),
                    meta: {breadCrumb: ["配置中心", "后台配置", "后台页面配置"]}
                },
                {
                    path: "bg/server",
                    component: () => import("../viewsAdmin/index/setting/bg/bgServer/index.vue"),
                    meta: {breadCrumb: ["配置中心", "后台配置", "服务协议"]}
                },
                {
                    path: "/adoption/manage",
                    component: () => import("../viewsAdmin/index/adoption/adoptionManage/productManage/index.vue"),
                    meta: {breadCrumb: ["认养中心", "认养产品", "产品管理"]}
                },
                {
                    path: "/adoption/adoptionManage/adoptionPackage",
                    component: () => import("../viewsAdmin/index/adoption/adoptionManage/adoptionProduct/index.vue"),
                    meta: {breadCrumb: ["认养中心", "认养管理", "认养套餐"]},

                },
                {
                    path: "adoption/category",
                    component: () => import("../viewsAdmin/index/adoption/adoptionManage/categoryManage/index.vue"),
                    meta: {breadCrumb: ["认养中心", "认养管理", "分类管理"]}
                },
                {
                    path: "adoption/crops",
                    component: () => import("../viewsAdmin/index/adoption/adoptionManage/cropsManage/index.vue"),
                    meta: {breadCrumb: ["认养中心", "认养管理", "农作物管理"]}
                },
                {
                    path: "adoption/seed",
                    component: () => import("../viewsAdmin/index/adoption/adoptionManage/seedManage/index.vue"),
                    meta: {breadCrumb: ["认养中心", "认养管理", "种子管理"]}
                },
                {
                    path: "shops/category",
                    component: () => import("../viewsAdmin/index/shops/shopsManage/shopsCategory/index.vue"),
                    meta: {breadCrumb: ["商品中心", "商品管理", "商品分类"]}
                },
                {
                    path: "member/manage",
                    component: () => import("../viewsAdmin/index/member/memberManage/index.vue"),
                    meta: {breadCrumb: ["会员中心", "会员管理"]}
                },
            ]
        },
        {
            path: "/login",
            component: () => import("../viewsAdmin/login/login.vue"),
        },
        {
            path: "/*",
            redirect: "/"
        }
    ]
})


router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token');
    if (to.meta && to.meta.title) {
        document.title = to.meta.title;
    }
    if (to.path !== '/login' && !token) {
        next('/login');
    } else {
        next();
    }
});
const routerData = localStorage.getItem("routers");
// 如果routerData存在且有效，添加动态路由
if (routerData) {
    const parsedRouterData = JSON.parse(routerData);
    parsedRouterData.forEach(route => {
        if (route.component) {
            router.addRoute(route);
        }
    });
}

// 重写push和replace方法
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};

export default router;
